package org.example;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class client {
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 10086);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("客服端连接成功");
        while (true) {

            System.out.println("==============欢迎来到黑马程序员=============");
            System.out.println("1登录");
            System.out.println("2注册");
            System.out.println("请输入您的选择：");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    System.out.println("选择了登录");

                    login(bw, br);
                    break;
                case 2:
                    System.out.println("选择了注册");
                    register(bw, br);
                    break;
                default:
                    System.out.println("无效命令");
            }
        }
    }

    private static void register(BufferedWriter bw, BufferedReader br) throws IOException {
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名");
            String name = sc.nextLine(); // 读取用户名

            System.out.println("请输入密码");
            String password = sc.nextLine(); // 读取密码

            System.out.println("用户名: " + name);
            System.out.println("密码: " + password);
            sendMessage(bw, name + "&" + password+"&2");
            String s = receiveMessage(br);
            switch (s) {
                case "1":
                    System.out.println("注册成功");
                    System.out.println("请登录");
                    return;
                case "2":
                    System.out.println("用户名存在请从新输入");
                    break;
                case "3":
                    System.out.println("用户名密码格式错误，请从新输入");
                    System.out.println("用户名格式：长度：6~18位，纯字母，不能有数字或其他符号。");
                    System.out.println("密码格式：密码长度3~8位。第一位必须是小写或者大小的字母，后面必须是纯数字");
            }
        }
    }

    public static void login(BufferedWriter bw, BufferedReader br) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = sc.nextLine(); // 读取用户名

        System.out.println("请输入密码");
        String password = sc.nextLine(); // 读取密码

        System.out.println("用户名: " + name);
        System.out.println("密码: " + password);
        sendMessage(bw, name + "&" + password+"&1");
        String s = receiveMessage(br);
        switch (s) {
            case "1":
                System.out.println("登录成功");
                while (true) {
                    new Thread(new ClientRunnable(br)).start();
                    System.out.println("请输入您要发送的消息");
                    String str = sc.nextLine();
                    sendMessage(bw, str);
                }
            case "2":
                System.out.println("用户名不存在请注册");
                break;
            case "3":
                System.out.println("密码错误，请从新选择登录");
        }

    }

    public static void sendMessage(BufferedWriter bw, String code) throws IOException {
        bw.write(code);
        bw.newLine();
        bw.flush();
    }

    public static String receiveMessage(BufferedReader br) throws IOException {
        String s = br.readLine();
        return s;
    }

}

class ClientRunnable implements Runnable {
    private BufferedReader bufferedReader;

    public ClientRunnable(BufferedReader br) {
        bufferedReader = br;
    }

    @Override
    public void run() {
        try {
            String s = bufferedReader.readLine();
            System.out.println(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}